package com.yoosora.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import javax.xml.ws.spi.http.HttpContext;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class RequestUtils {
	HttpRequestBase httpRequestBase = null; 
	public String build(String url, Boolean isReferer, Map<String, String> referer, Map<String, String> param){
		CloseableHttpClient httpclient = HttpClients.createDefault();
		String resultString = "";
		CloseableHttpResponse response = null;
		try {
			URIBuilder builder = new URIBuilder(url);
			if(param!=null && param.size()>0){
				for(String key:param.keySet()){
					builder.addParameter(key,param.get(key));
				}
			}
			URI uri = builder.build();
			httpRequestBase.setURI(uri);
			if(isReferer!=null&&isReferer){
				if(referer!=null&&referer.size()>0){
					for(String key:referer.keySet()){
						httpRequestBase.addHeader(key,referer.get(key));
					}
				}
			}
			response = httpclient.execute(httpRequestBase);
			if (response.getStatusLine().getStatusCode() == 200) {
				resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
			} 
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if (response != null) {
					response.close();
				}
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			} 
		}
		return resultString;
	}
	public String buildLocationStr(String url, Boolean isReferer, Map<String, String> referer, Map<String, String> param){
		CloseableHttpClient httpclient = HttpClients.createDefault();
		String resultString = "";
		CloseableHttpResponse response = null;
		try {
			URIBuilder builder = new URIBuilder(url);
			if(param!=null && param.size()>0){
				for(String key:param.keySet()){
					builder.addParameter(key,param.get(key));
				}
			}
			URI uri = builder.build();
			httpRequestBase.setURI(uri);
			if(isReferer!=null&&isReferer){
				if(referer!=null&&referer.size()>0){
					for(String key:referer.keySet()){
						httpRequestBase.addHeader(key,referer.get(key));
					}
				}
			}
			HttpClientContext httpContext = new HttpClientContext();
			response = httpclient.execute(httpRequestBase,httpContext);
			int responseCode = response.getStatusLine().getStatusCode();
			if(responseCode == 200){
				List<URI> redirectLocations = httpContext.getRedirectLocations();
				resultString = redirectLocations.get(0).toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if (response != null) {
					response.close();
				}
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return resultString;
	}
	public RequestUtils get(){
		httpRequestBase = new HttpGet();
		return this;
	}
	public RequestUtils post(){
		httpRequestBase = new HttpPost();
		return this;
	}
	public static RequestUtils builder(){
		return new RequestUtils();
	}
}
